import org.htmlparser.Node;
import org.htmlparser.Tag;
import org.htmlparser.tags.HeadingTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TitleTag;
import org.htmlparser.visitors.NodeVisitor;


public class Visitors {

	public NodeVisitor visitor = new NodeVisitor(){
		
		public void visitTag(Tag tag){
			
			//page title to extract project name
			if(tag instanceof TitleTag){
				String title = ((TitleTag) tag).getStringText();
				webParser.ProjectNameList.add(title);
			}
			//head line to extract project name
			if(webParser.ProjectNameList.size() <= 1 && tag instanceof HeadingTag){
				String headline = ((HeadingTag) tag).getStringText();
				webParser.ProjectNameList.add(headline);
			}
			
			//hyper link to extract mailing list address
			if(tag instanceof LinkTag){
				//the link address
				String link = ((LinkTag) tag).extractLink();
				//System.out.println(link);
				
				//link text contains "mailing list"
				if(((LinkTag) tag).getLinkText().toLowerCase().contains("mailing list")){
					webParser.MailingList.add(link);
				}
				//previous sibling contains "mailing list"
				else if(tag.getPreviousSibling() != null){
					Node sibling = tag.getPreviousSibling();
					System.out.println("@@@@@@@@@@@@@@@@");
					System.out.println(tag.toHtml());
					if(sibling.getParent() != null){
						System.out.println("$$$" + sibling.getParent().toHtml());
						if(sibling.getParent().getPreviousSibling().getParent() != null)
							System.out.println("%%%" + sibling.getParent().getPreviousSibling().getParent().getText());
					}
					if(sibling.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				//
				else if(tag.getParent() != null){
					Node parent = tag.getParent();
					//System.out.println(parent.toHtml());
					if(parent.getPreviousSibling() != null){
						//System.out.println(parent.getPreviousSibling().toPlainTextString());
					}
					if(parent.getParent() != null){
						/*System.out.println("@@@@@@@@@@@@@@@@");
						System.out.println(parent.getParent().toHtml());
						System.out.println("%%%%%%%%%%%%%%%%");
						System.out.println("~~~~~~~~~~~~~~~" + link);*/
					}
					//if(parent.toPlainTextString().toLowerCase().contains("mailing list"))
				}
			}
		}
	};
	
	public NodeVisitor test = new NodeVisitor(){
		
		int minDistance = Integer.MAX_VALUE;
		
		public void visitTag(Tag tag){
			if(tag instanceof LinkTag){
				//the link address
				String link = ((LinkTag) tag).extractLink();
				//System.out.println("LINK           " + link);
				
				Node tmp;
				if(tag.getParent() != null){
					//System.out.println("PARENT         " + tag.getParent().toHtml());
					tmp = tag.getParent();
					if(tmp.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				if(tag.getPreviousSibling() != null){
					//System.out.println("PSIBLING       " + tag.getPreviousSibling().toHtml());
					tmp = tag.getPreviousSibling();
					if(tmp.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				if(tag.getNextSibling() != null){
					//System.out.println("NSIBLING       " + tag.getNextSibling().toHtml());
					tmp = tag.getNextSibling();
					if(tmp.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				if(tag.getParent().getPreviousSibling()!= null){
					//System.out.println("PARENT PSIBLING" + tag.getParent().getPreviousSibling().toHtml());
					tmp = tag.getParent().getPreviousSibling();
					if(tmp.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				if(tag.getParent().getParent() != null){
					//System.out.println("PARENT PARENT  " + tag.getParent().getParent().toPlainTextString());
					tmp = tag.getParent().getParent();
					System.out.println(tmp.getStartPosition());
					if(tmp.toPlainTextString().toLowerCase().contains("mailing list"))
						webParser.MailingList.add(link);
				}
				
			}
		}
	};
}
